package com.night.xjh.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.night.common.entity.ResultVo;
import com.night.common.entity.xjh.Header;
import com.night.common.entity.xjh.HiddenTrouble;
import com.night.xjh.vo.HeaderVo;
import com.night.xjh.vo.HiddenTroubleVo;
import com.night.xjh.service.HeaderService;
import com.night.xjh.service.HiddenTroubleService;
import com.night.xjh.service.HiddenTroubleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HiddenTroubleController
 * @Description: //TODO
 * @author: xjh
 * @date: 2022/10/24-9:12
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/xjh/hidden")
public class HiddenTroubleController {
    @Autowired
    private HiddenTroubleService hiddenTroubleService;
    @Autowired
    private HiddenTroubleTypeService hiddenTroubleTypeService;
    @Autowired
    private HeaderService headerService;
    @RequestMapping("/add")
    @SentinelResource(value = "add",blockHandler = "withBlockHandler")
    public ResultVo<?> add(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        hiddenTroubleService.add(dataFormat(hiddenTroubleVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/update")
    public ResultVo<?> update(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        hiddenTroubleService.update(dataFormat(hiddenTroubleVo));
        return ResultVo.SUCCESS;
    }
    @RequestMapping("/getAll")
    public ResultVo<?> getAll(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        return new ResultVo<>(hiddenTroubleService.getAll(
                hiddenTroubleVo.getPageNum(),
                hiddenTroubleVo.getPageSize()
        ));
    }
    @RequestMapping("/getByCondition")
    public ResultVo<?> getByCondition(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        if(hiddenTroubleVo.getDealStatus().equals("0")){
            hiddenTroubleVo.setDealStatus("");
        }
        return new ResultVo<>(hiddenTroubleService.getByCondition(
                dataFormat(hiddenTroubleVo),
                hiddenTroubleVo.getPageNum(),
                hiddenTroubleVo.getPageSize()
        ));
    }
    @RequestMapping("/getById")
    public ResultVo<?> getById(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        return new ResultVo<>(hiddenTroubleService.getById(
                hiddenTroubleVo.getId()
        ));
    }
//--------------------Type-------------,getById,getAll
    @RequestMapping("/getTypeById")
    public ResultVo<?> getTypeById(@RequestBody HiddenTroubleVo hiddenTroubleVo){
        return new ResultVo<>(hiddenTroubleTypeService.getById(
                hiddenTroubleVo.getHiddenTroubleTypeId()
        ));
    }
    @RequestMapping("/getAllType")
    public ResultVo<?> getAllType(){
        return new ResultVo<>(hiddenTroubleTypeService.getAll());
    }


    private HiddenTrouble dataFormat(HiddenTroubleVo hiddenTroubleVo){
        HiddenTrouble hiddenTrouble=new HiddenTrouble();
        hiddenTrouble.setId(hiddenTroubleVo.getId());
        hiddenTrouble.setName(hiddenTroubleVo.getName());
        hiddenTrouble.setHiddenTroubleType(hiddenTroubleTypeService.getById(hiddenTroubleVo.getHiddenTroubleTypeId()));
        hiddenTrouble.setPlace(hiddenTroubleVo.getPlace());
        hiddenTrouble.setDescription(hiddenTroubleVo.getDescription());
        hiddenTrouble.setReportDate(hiddenTroubleVo.getReportDate());
        hiddenTrouble.setHeader(headerService.getById(hiddenTroubleVo.getHeaderId()));
        hiddenTrouble.setDealStatus(hiddenTroubleVo.getDealStatus());
        hiddenTrouble.setEndDate(hiddenTroubleVo.getEndDate());
        hiddenTrouble.setReportPeople(hiddenTroubleVo.getReportPeople());
        hiddenTrouble.setPhone(hiddenTroubleVo.getPhone());
        return hiddenTrouble;
    }
}
